package edu.hebeu.util.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * 这个类用来操作数据库的增删改查
 */
public class Dao {

    private final SQLiteDataBaseHelper dbHelper;

    public Dao(Context context) {
        dbHelper = new SQLiteDataBaseHelper(context); // 创建数据库
    }

    /**
     * 插入数据
     * @param id
     * @param name
     * @param store
     * @param buy_num
     * @param price
     * @param sell_username
     */
    public long insert(String id, String name, String store, String buy_num, String price,
               String sell_username, String src, String describe, String index, String goods_category_id) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("id", id);
        values.put("name", name);
        values.put("store", store);
        values.put("buy_num", buy_num);
        values.put("price", price);
        values.put("sell_username", sell_username);
        values.put("src", src);
        values.put("describe", describe);
        values.put("_index", index);
        values.put("goods_category_id", goods_category_id);
        /*表示操作 SQLiteConfig.TABLE_NAME表；如果未写入值，就为null；插入的值在values内封装
         */
        long insertNum = db.insert(SQLiteConfig.TABLE_NAME, null, values);

        db.close();
        return insertNum;
    }

    public int delete(String id) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        int delNum = db.delete(SQLiteConfig.TABLE_NAME, "id=?", new String[] {id});
//        int delNum = db.delete(SQLiteConfig.TABLE_NAME, null, null);

        db.close();
        return delNum;
    }

    public int update(String id, String name, String store, String buy_num, String price,
                String sell_username, String src, String describe, String index, String goods_category_id) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("id", id);
        values.put("name", name);
        values.put("store", store);
        values.put("buy_num", buy_num);
        values.put("price", price);
        values.put("sell_username", sell_username);
        values.put("src", src);
        values.put("describe", describe);
        values.put("_index", index);
        values.put("goods_category_id", goods_category_id);
        int updateNum = db.update(SQLiteConfig.TABLE_NAME, values, "id=?", new String[]{id});

        db.close();
        return updateNum;
    }

    public List<String[]> query() {
        List<String[]> result = new ArrayList<>();

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query(SQLiteConfig.TABLE_NAME, null, null,
                null, null, null, null);

        if (cursor.getCount() == 0) { // 如果未查询到数据
        } else { // 否则，即有数据
            while (cursor.moveToNext()) {
                String id = cursor.getString(0);
                String name = cursor.getString(1);
                String store = cursor.getString(2);
                String buy_num = cursor.getString(3);
                String price = cursor.getString(4);
                String sell_username = cursor.getString(5);
                String src = cursor.getString(6);
                String describe = cursor.getString(7);
                String index = cursor.getString(8);
                String goods_category_id = cursor.getString(9);

                String[] strings = new String[10];
                strings[0] = id;
                strings[1] = name;
                strings[2] = store;
                strings[3] = buy_num;
                strings[4] = price;
                strings[5] = sell_username;
                strings[6] = src;
                strings[7] = describe;
                strings[8] = index;
                strings[9] = goods_category_id;
                result.add(strings);
            }
        }
        cursor.close();
        db.close();
        return result;
    }

//    public Map<String, String[]> query2() {
//        Map<String, String[]> result = new HashMap<>();
//        SQLiteDatabase db = dbHelper.getWritableDatabase();
//        Cursor cursor = db.query(SQLiteConfig.TABLE_NAME, null, null,
//                null, null, null, null);
//
//        if (cursor.getCount() == 0) { // 如果未查询到数据
//        } else { // 否则，即有数据
//            while (cursor.moveToNext()) {
//                String id = cursor.getString(0);
//                String name = cursor.getString(1);
//                String store = cursor.getString(2);
//                String buy_num = cursor.getString(3);
//                String price = cursor.getString(4);
//                String sell_username = cursor.getString(5);
//                String src = cursor.getString(6);
//            }
//        }
//
//        cursor.close();
//        db.close();
//        return result;
//    }
}
